<template>
  <div class="articles">
    <banner></banner>
    <div class="site-content animate">
      <!-- 文章目录 -->
      <div id="article-menus">
        <menu-tree :menus="post" child-label="child"></menu-tree>
      </div>
      <main class="site-main">
        <article class="hentry">
          <!-- 文章头部 -->
          <header class="entry-header">
            <!-- 标题输出 -->
            <h1 class="entry-title">{{ post.title }}</h1>
            <hr />
            <div class="breadcrumbs">
              <div id="crumbs">最后更新时间：{{ post.updateTime }}</div>
            </div>
          </header>
           <div class="entry-content">

          <div v-html="post.text"></div>
          <!-- 正文输出 -->
          <!--             <div class="entry-content">
                        <p>@[TOC]</p>
                        <h4 id="引言：">引言：</h4>
                        <p>Lua 是一种轻量小巧的脚本语言，能为应用程序提供灵活的扩展和定制功能。</p>
                        <h4 id="lua-应用场景">Lua 应用场景</h4>
                        <ul>
                            <li>游戏开发</li>
                            <li>独立应用脚本</li>
                            <li>Web 应用脚本</li>
                            <li>扩展和数据库插件如：MySQL Proxy 和 MySQL WorkBench</li>
                            <li>安全系统，如入侵检测系统</li>
                        </ul>
                        <hr>
                        <h4 id="安装">安装</h4>
                        <pre><code> curl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gz #下载
 tar zxf lua-5.3.5.tar.gz #解压
 cd lua-5.3.3 #进入解压文件夹
 make linux test #安装&amp;测试</code></pre>
                        <hr>
                        <h4 id="安装过程可能出现的问题">安装过程可能出现的问题</h4>
                        <ol>
                            <li>没有gcc命令(lua是C语言编写的，安装时依赖gcc)
                                <blockquote>
                                    <p>使用<code>which gcc</code>命令可以查看是否有gcc，如果没有使用下面命令进行安装gcc 与gcc-c++，</p>
                                    <pre><code>yum -y install gcc
yum -y install gcc-c++</code></pre>
                                </blockquote>
                            </li>
                        </ol>
                        <hr>
                        <ol start="2">
                            <li>致命错误：readline/readline.h：没有那个文件或目录
                                <blockquote>
                                    <p>执行如下命令即可:</p>
                                    <pre><code>yum install libtermcap-devel ncurses-devel libevent-devel readline-devel</code></pre>
                                </blockquote>
                            </li>
                        </ol>
                        <hr>
                        <h4 id="安装成功验证">安装成功验证</h4>
                        <blockquote>
                            <p>执行<code>lua -v</code>,出现如下信息代表安装成功<br/>
                                Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio</p>
                        </blockquote>
                        <pre><code>lua -v</code></pre>

                    </div> -->
          <!-- 文章底部 -->
            </div>
          <section-title>
            <footer class="post-footer">
              <!-- 阅读次数 -->
              <!--   <div class="post-like">
                                <i class="iconfont iconeyes"></i>
                                <span class="count">685</span>
                            </div> -->
              <!-- 分享按钮 -->
              <!--                        <div class="post-share">-->
              <!--                            <ul class="sharehidden">-->
              <!--                                <li><a href="http://qr.liantu.com/api.php?text=https://zhebk.cn/Web/gongan.html"-->
              <!--                                       onclick="window.open(this.href, 'renren-share', 'width=490,height=700');return false;"-->
              <!--                                       class="s-weixin" target="_blank" rel="nofollow noopener noreferrer"><img src="https://cdn.zhebk.cn/usr/themes/Akina/images/wechat.png"></a></li>-->
              <!--                                <li>-->
              <!--                                    <a href="http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=https://zhebk.cn/Web/gongan.html/&amp;title=公安联网备案记录"-->
              <!--                                       onclick="window.open(this.href, 'weibo-share', 'width=730,height=500');return false;"-->
              <!--                                       class="s-qq" target="_blank" rel="nofollow noopener noreferrer"><img src="https://cdn.zhebk.cn/usr/themes/Akina/images/qzone.png"></a></li>-->
              <!--                                <li>-->
              <!--                                    <a href="http://service.weibo.com/share/share.php?url=https://zhebk.cn/Web/gongan.html/&amp;title=公安联网备案记录"-->
              <!--                                       onclick="window.open(this.href, 'weibo-share', 'width=550,height=235');return false;"-->
              <!--                                       class="s-sina" target="_blank" rel="nofollow noopener noreferrer"><img src="https://cdn.zhebk.cn/usr/themes/Akina/images/sina.png"></a></li>-->
              <!--                                <li>-->
              <!--                                    <a href="http://shuo.douban.com/!service/share?https://zhebk.cn/Web/gongan.html/&amp;title=公安联网备案记录"-->
              <!--                                       onclick="window.open(this.href, 'renren-share', 'width=490,height=600');return false;"-->
              <!--                                       class="s-douban" target="_blank" rel="nofollow noopener noreferrer"><img src="https://cdn.zhebk.cn/usr/themes/Akina/images/douban.png"></a></li>-->
              <!--                            </ul>-->
              <!--                            <i class="iconfont show-share"></i>-->
              <!--                        </div>-->
              <!-- 赞助按钮 -->
              <div class="donate" @click="showDonate = !showDonate">
                <span>赏</span>
                <ul class="donate_inner" :class="{ show: showDonate }">
                  <li class="wedonate">
                    <img
                      src="https://gitee.com/ajsnsbdbdn/notes-collection/raw/master/IMG_20210304_165816.jpg"
                    />
                    <p>微信</p>
                  </li>
                </ul>
              </div>
              <!-- 文章标签 -->
              <div class="post-tags">
                <i class="iconfont iconcategory"></i>
                <span>{{ post.category.tltle }}</span>
              </div>
            </footer>
          </section-title>

          <!--声明-->
          <div class="open-message">
            <p>
              声明：Li博客|版权所有，违者必究|如未注明，均为原创|本网站采用<a
                href="https://creativecommons.org/licenses/by-nc-sa/3.0/"
                target="_blank"
                >BY-NC-SA</a
              >协议进行授权
            </p>
            <p>
              转载：转载请注明原文链接 -
              <a href="/">{{ post.title }}</a>
            </p>
          </div>
          <!--评论-->
          <div class="comments">
            <span
              class="spanClass"
              @click.stop="
                user.fatherId = 0;
                showCommentEditor = true;
              "
              style="cursor: pointer"
              >回复帖子</span
            >
            <span>@回复最多三条</span>
            <div class="comment-body">
              <div
                v-if="showCommentEditor"
                @click.stop="showCommentEditor = true"
              >
                <div class="user-avatar">
                  <img class="imagesotu" :src="user.image" />
                </div>
                QQ:<input
                  @blur="getName"
                  v-on:input="qqinupt"
                  v-model="qq"
                  placeholder="请输入qq号"
                />
                昵称:<input v-model="user.name" />
                <comment-message-editor
                  :inline="true"
                  v-model="user.text"
                  buttonText="回复"
                  @submit="submitReply"
                ></comment-message-editor>
              </div>
              <slot></slot>
            </div>
            <comment v-for="item in comments" :key="item.id" :comment="item">
              <template v-if="item.messages[0] != undefined">
                <comment
                  v-for="reply in item.messages"
                  :key="reply.id"
                  :comment="reply"
                >
                  <template v-if="reply.messages[0] != undefined">
                    <comment
                      v-for="reply1 in reply.messages"
                      :key="reply1.id"
                      :comment="reply1"
                    ></comment>
                  </template>
                  -
                </comment>
              </template>
              -
            </comment>
          </div>
        </article>
      </main>
    </div>
  </div>
</template>
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
import Banner from "@/components/banner";
import qq from "@/api/qq";
import message from "@/api/message";
import commentMessageEditor from "comment-message-editor";
import sectionTitle from "@/components/section-title";
import comment from "@/components/comment";
import menuTree from "@/components/menu-tree";
import { fetchComment } from "../api";
import post from "@/api/post";
var df={
        text: "", //留言内容
        image: "http://q2.qlogo.cn/headimg_dl?dst_uin=1&spec=100", //留言人头像
        name: "", //留言名
        fatherId: 0, //回复人id
        postId: 0,
        fatherName: "", //回复人名
      }
export default {
  name: "articles",
  data() {
    return {
      showDonate: false,
      comments: [],
      menus: [],
      post: {},
      id: 0,
      showCommentEditor: false,
      user: df,
      qq: "",
    };
  },
  components: {
    Banner,
    sectionTitle,
    comment,
    menuTree,
    sectionTitle,
    commentMessageEditor,
  },
  watch: {
    showCommentEditor(value) {
      if (value) {
        document.body.addEventListener("click", this.close);
      } else {
        document.body.removeEventListener("click", this.close);
      }
    },
  },
  methods: {
    stopUser(id) {
      showCommentEditor = true;
      //console.log(id);
    },
    getName() {
      qq.getQQname(this.qq).then((res) => {
        this.user.name = res.data.qq;
      });
    },

    qqinupt() {
      this.user.image =
        "http://q2.qlogo.cn/headimg_dl?dst_uin=" + this.qq + "&spec=100";
    },
    reply(id) {
      const ref = `comment${id}`;
    },
    submitReply(v) {
      this.user.text = v;
      this.user.postId = this.$route.params.id;
      //console.log(this.user)
      message.saveMessage(this.user).then((res) => {
        this.$router.go(0);
      });
    },
    close() {
      this.showCommentEditor = false;
      this.user={...df}
      this.qq=""
    },
    getComment() {
      const id = this.$route.params.id;
      this.id = id;
      post.getById(id).then((res) => {
        this.post = res.data.data;
        this.comments = res.data.data.messages;
        //console.log(this.comments);
      });
    },
    fetchH(arr, left, right) {
      if (right) {
        return arr.filter(
          (item) => item.offsetTop > left && item.offsetTop < right
        );
      } else {
        return arr.filter((item) => item.offsetTop > left);
      }
    },
    // 生成目录
    createMenus() {
      let arr = [];
      for (let i = 6; i > 0; i--) {
        let temp = [];
        let e = document
          .querySelector(".entry-content")
          .querySelectorAll(`h${i}`);
        for (let j = 0; j < e.length; j++) {
          let child = this.fetchH(
            arr,
            e[j].offsetTop,
            j + 1 === e.length ? undefined : e[j + 1].offsetTop
          );
          temp.push({
            h: i,
            title: e[j].innerText,
            id: e[j].id,
            offsetTop: e[j].offsetTop,
            child,
          });
        }
        if (temp.length) {
          arr = temp;
        }
      }
      this.menus = arr;
    },
  },
  mounted() {
    this.createMenus();
  },
  created() {
    this.getComment();
  },
};
</script>
<style scoped lang="less">
.spanClass {
  position: relative;
  left: 50%;
}
.site-content {
  position: relative;
  .site-main {
    padding: 80px 0 0 0;
  }
}
#article-menus {
  position: sticky;
  top: 0;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  border-radius: 3px;
  padding: 15px;
  width: 300px;
  transform: translateX(-120%) translateY(150px);
  font-size: 14px;
}
article.hentry {
  .entry-header {
    .entry-title {
      font-size: 23px;
      font-weight: 600;
      color: #737373;
      margin: 0.67em 0;

      &:before {
        content: "#";
        margin-right: 6px;
        color: #d82e16;
        font-size: 20px;
        font-weight: 600;
      }
    }

    hr {
      height: 1px;
      border: 0;
      background: #efefef;
      margin: 15px 0;
    }

    .breadcrumbs {
      font-size: 14px;
      color: #d2d2d2;
      text-decoration: none;
      margin-bottom: 30px;
    }
  }

  .entry-content {
  }

  footer.post-footer {
    width: 100%;
    padding: 20px 10px;
    margin-top: 30px;
    height: 65px;
    position: relative;
    i {
      font-size: 18px;
      margin-right: 5px;
    }
    .post-like {
      float: right;
      margin: 7px 0 0 20px;
    }

    .post-share {
      float: right;
      list-style: none;
      margin-right: 20px;
    }

    .donate {
      float: left;
      line-height: 36px;
      border-radius: 100%;
      -webkit-border-radius: 100%;
      -moz-border-radius: 100%;
      border: 1px solid #2b2b2b;
      &:hover {
        border: 1px solid goldenrod;
        span {
          color: goldenrod;
        }
      }
      span {
        color: #2b2b2b;
        padding: 10px;
        position: relative;
        cursor: pointer;
      }

      .donate_inner {
        display: none;
        margin: 0;
        list-style: none;
        position: absolute;
        left: 80px;
        top: -40px;
        background: #fff;
        padding: 10px;
        border: 1px solid #ddd;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
        border-radius: 3px;
        &.show {
          display: block;
        }
        li {
          float: left;
        }

        img {
          width: 100px;
        }
        p {
          text-align: center;
          font-size: 15px;
          color: #d2d2d2;
          line-height: 1rem;
        }
      }

      .donate_inner:after,
      .donate_inner:before {
        content: "";
        position: absolute;
        left: 0;
        bottom: 45%;
        margin-left: -8px;
        border-top: 8px solid transparent;
        border-bottom: 8px solid transparent;
        border-right: 8px solid #fff;
      }

      .donate_inner:before {
        left: -1px;
        border-right: 8px solid #ddd;
      }
    }

    .post-tags {
      margin: 7px 0 0 20px;
      float: left;
      text-transform: uppercase;
      a:hover {
        color: #ff6d6d;
      }
    }
  }
  .open-message {
    margin: 50px 0;
    position: relative;
    background: #2b2b2b;
    padding: 10px 30px;
    border-radius: 3px;
    font-size: 14px;
    color: #fff;
    &:after {
      content: "";
      border-left: 10px solid transparent;
      border-right: 10px solid transparent;
      border-bottom: 10px solid #2b2b2b;
      position: absolute;
      top: -8px;
      left: 48%;
    }
    p {
      margin: 10px 0;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    a {
      color: #a0dad0;
      padding: 0 5px;
    }
  }
}
</style>
